package com.sectionlistener;


import com.sectionlistener.fragments.MusicListFragment;
import com.sectionlistener.fragments.PlayerFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;

import android.view.Menu;
import android.view.MotionEvent;

public class MainActivity extends FragmentActivity implements MusicListFragment.OnListViewEventListener{
	public static final String ARG_PAGE = "page";
	
    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;
    
    /**
     * The number of pages (wizard steps) to show in this demo.
     */
    private static final int NUM_PAGES = 2;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When changing pages, reset the action bar actions since they are dependent
                // on which page is currently active. An alternative approach is to have each
                // fragment expose actions itself (rather than the activity exposing actions),
                // but for simplicity, the activity provides the actions in this sample.
            	supportInvalidateOptionsMenu();
            }
        });
    }

/*    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }*/
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	return false;
	}
    
//    private void exchangeFragment(int flag) {
////    	
////    	//getSupportFragmentManager().popBackStack();
////    	
////    	FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction(); 
////
//////    	if( flag == 1)
////    		transaction.setCustomAnimations(R.layout.slide_in_right, R.layout.slide_out_right);
//////    	transaction.setCustomAnimations(R.layout.slide_in_right, 0);
//////    	transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
////    	
//////    	transaction.set
////    	// Replace whatever is in the fragment_container view with this fragment,
////    	// and add the transaction to the back stack
////    	transaction.replace(R.id.fragment_container, fragNext);
////    	transaction.addToBackStack(null);
////    	
////    	// Commit the transaction
////    	transaction.commit();
////    	
////    	Fragment fragTemp = fragNext;
////    	fragNext = fragCurr;
////    	fragCurr = fragTemp;
//    }

	@Override
	public void onTouch(int fragID) {
		// TODO Auto-generated method stub
		
	}
	
    /**
     * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
       		return create(position);
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
    static Fragment create(int pageNumber)
    {
    	if(pageNumber == 0)
    	{
    		MusicListFragment fragment = new MusicListFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_PAGE, pageNumber);
            fragment.setArguments(args);
            return fragment;
    	}
    	//	return new ListFragment();
    	else
    	{
    		PlayerFragment fragment = new PlayerFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_PAGE, pageNumber);
            fragment.setArguments(args);
            return fragment;
    	}
    }
}
